package com.caldremch.androidfoundation.http.request;

import android.text.TextUtils;


import com.caldremch.androidfoundation.http.ApiService;
import com.caldremch.androidfoundation.http.callback.AbsCallback;
import com.caldremch.androidfoundation.http.func.ApiRetryFunc;

import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 *
 * @author Caldremch
 * @date 2018/11/6
 * @Email caldremch@163.com
 * @describe 通用请求器
 * @param <R> type
 **/

public abstract class BaseHttpRequest<R extends BaseHttpRequest> extends BaseRequest<R> {

    protected ApiService mApiService; //通用接口服务
    protected String mSuffixUrl = ""; //链接后缀
//    protected int retryDelayMillis;//请求失败重试间隔时间
//    protected int retryCount;//重试次数
    protected Map<String, String> mParams = new LinkedHashMap<>(); //请求参数

    public BaseHttpRequest() {
    }

    public BaseHttpRequest(String suffixUrl) {
        if (!TextUtils.isEmpty(suffixUrl)) {
            this.mSuffixUrl = suffixUrl;
        }
    }

    public <T> Observable<T> request(Type type) {
        newConfig();
        return execute(type);
    }

    public <T> void request(AbsCallback<T> callback) {
        newConfig();
        execute(callback);
    }

    @Override
    protected void newConfig() {
        super.newConfig();
        mApiService = mRetrofit.create(ApiService.class);

    }



    protected abstract <T> Observable<T> execute(Type type);

    protected abstract <T> void execute(AbsCallback<T> callback);

//    protected <T> ObservableTransformer<ResponseBody, T> norTransformer(final Type type) {
//        return new ObservableTransformer<ResponseBody, T>() {
//            @Override
//            public ObservableSource<T> apply(Observable<ResponseBody> apiResultObservable) {
//                return apiResultObservable
//                        .subscribeOn(Schedulers.io())
//                        .unsubscribeOn(Schedulers.io())
//                        .map(new ApiFunc<T>(type))
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .retryWhen(new ApiRetryFunc());
//            }
//        };
//    }

    /**
     * 添加请求参数
     */
    public R addParam(String paramKey, String paramValue) {
        if (paramKey != null && paramValue != null) {
            this.mParams.put(paramKey, paramValue);
        }
        return (R) this;
    }

    /**
     * 添加请求参数
     */
    public R addParams(Map<String, String> params) {
        if (params != null) {
            this.mParams.putAll(params);
        }
        return (R) this;
    }


    /**
     * 设置请求参数
     */
    public R params(Map<String, String> params) {
        if (params != null) {
            this.mParams = params;
        }
        return (R) this;
    }


    public String getSuffixUrl() {
        return mSuffixUrl;
    }


    public Map<String, String> getParams() {
        return mParams;
    }

}
